﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ASMS.Excel2DbTool.Utils
{
    /// <summary>
    /// 六廊六路
    /// </summary>
    public class 六廊六路
    {
        public static void Run(string filePath, string destFileName)
        {
            DataTable dataTable = Sunboxsoft.Excel.ReadTool.ReadExcel(filePath, true);

            StringBuilder coordJsonBuilder = new StringBuilder("{");
            StringBuilder projectJsonBuilder = new StringBuilder("[");

            Dictionary<string, List<Item>> dict = new Dictionary<string, List<Item>>();

            for (int index = 0; index < dataTable.Rows.Count; index++)
            {
                string dest = Convert.ToString(dataTable.Rows[index][8]).Trim();
                string destGEO = Convert.ToString(dataTable.Rows[index][11]).Trim();
                string type = Convert.ToString(dataTable.Rows[index][12]).Trim();

                if (string.IsNullOrEmpty(destGEO)
                    || string.IsNullOrEmpty(dest)
                    || string.IsNullOrEmpty(type))
                    continue;

                if (string.Equals(type, "其他"))
                {
                    continue;
                }

                string[] destCoordArr = destGEO.Split(new char[] { ',' });
                double geoLng = 0;
                double geoLat = 0;
                if (destCoordArr.Length == 2)
                {
                    if (double.TryParse(destCoordArr[0], out geoLng)
                        && double.TryParse(destCoordArr[1], out geoLat))
                    {
                    }
                    else
                    {
                        continue;
                    }
                    if (dict.ContainsKey(type))
                    {
                        dict[type].Add(new Item()
                        {
                            City = dest,
                            Geo = new double[] { geoLng, geoLat }
                        });
                    }
                    else
                    {
                        dict.Add(type, new List<Item>());
                        dict[type].Add(new Item()
                        {
                            City = dest,
                            Geo = new double[] { geoLng, geoLat }
                        });
                    }
                }
            }
            StringBuilder builder = new StringBuilder();

            builder.Append("[");
            bool isFist = true;
            foreach (string key in dict.Keys)
            {
                List<Item> items = dict[key];
                if (isFist == true)
                {
                    isFist = false;
                }
                else
                {
                    builder.Append(",");
                }
                builder.AppendFormat("{{\"type\":\"{0}\", \"value\":[", key);
                for (int index = 0; index < items.Count; index++)
                {
                    Item item = items[index];
                    if (index != 0)
                    {
                        builder.Append(",");
                    }
                    builder.AppendFormat("{{\"name\":\"{0}\",\"value\":[{1},{2},0]}}", item.City, item.Geo[0], item.Geo[1]);
                    builder.AppendLine();
                }
                builder.Append("]}");
                builder.AppendLine();
            }
            builder.Append("]");
            string destFilePath = string.Format("{0}\\{1}", AppDomain.CurrentDomain.BaseDirectory, destFileName);
            System.IO.File.WriteAllText(destFilePath, builder.ToString());
            Console.WriteLine(string.Format("转换完毕{0},count:{1}", destFilePath, dataTable.Rows.Count));
        }

        public class Item
        {
            public string City { get; set; }
            public double[] Geo { get; set; }
        }
    }
}